package wpmp.extjs3.data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import wcommons.lang.JsonUtils;

/**
 * load this data style to the form
 * 
 * <pre>
 * formpanel.getForm().load({
 * });
 * </pre>
 * 
 * @author Wayne.Wang<5waynewang@gmail.com>
 * @since 4:53:13 PM Dec 19, 2012
 */
public class Form<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	private boolean success = true;

	private Map<String, Object> errors = new HashMap<String, Object>();

	private T data;

	public static final String ERROR_MSG_KEY = "msg";

	public Form() {
	}

	public Form(T data) {
		this.data = data;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public Map<String, Object> getErrors() {
		return errors;
	}

	public void setErrors(String key, Object value) {
		this.errors.put(key, value);
	}

	public void setErrorMsg(String errorMsg) {
		this.errors.put(ERROR_MSG_KEY, errorMsg);
	}

	@Override
	public String toString() {
		return JsonUtils.toJSONString(this);
	}

	public String toJSONString() {
		return this.toString();
	}

	public static <X> Form<X> newInstance(X data) {
		return new Form<X>(data);
	}
}
